package com.tuniu.agents.common.entity.itinerary;

import com.tuniu.agents.common.entity.itinerary.enums.ResourceStatus;
import com.tuniu.agents.common.entity.itinerary.enums.ResourceType;
import com.tuniu.agents.common.i18n.I18nConstants;
import com.tuniu.agents.message.MessageRender;
import com.tuniu.agents.util.I18nUtil;
import com.tuniu.agents.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
public abstract class Resource {

    /**
     * resource type
     */
    private ResourceType resourceType;

    /**
     * departure/use date, format: yyyy-MM-dd
     */
    private String useDate;

    /**
     * resource item data
     */
    private Object data;

    /**
     * resource item markdown
     */
    private String markdown;

    /**
     * resource price
     */
    private BigDecimal price;

    /**
     * resource status
     */
    private ResourceStatus status;
    public Resource(){}
    public Resource(ResourceType resourceType, String useDate, Object data, String markdown, BigDecimal price, ResourceStatus status) {
        this.resourceType = resourceType;
        this.useDate = useDate;
        this.data = data;
        this.setMarkdown(markdown);
        this.price = price;
        this.status = status;
    }

    /**
     * determine whether the input resource is match to this resource
     * @param resource input resource
     */
    public abstract boolean match(Resource resource);

    /**
     * get the file name of the template
     */
    public abstract String createTemplateName();

    /**
     * build markdown string
     */
    public String toMarkdown(MessageRender messageRender, String conversationId) {
        if (data == null) {
            return "";
        }

        Map<String, Object> dataMap = JsonUtil.toMap(JsonUtil.toJson(data));
        String fileName = createTemplateName();
        if (!StringUtils.hasText(fileName)) {
            return "";
        }

        String markdown = null;
        try {
            markdown = messageRender.renderTemplate(fileName, Map.of(I18nConstants.LANGUAGE_TAG, I18nUtil.getLocaleLanguageTag(conversationId)), dataMap);
        } catch (Exception e) {
            log.error("conversationId:{}, build markdown failed, e:{}, date:{}", conversationId, e.getMessage(), data);
        }
        return markdown;
    }


    public ResourceType getResourceType() {
        return resourceType;
    }

    public void setResourceType(ResourceType resourceType) {
        this.resourceType = resourceType;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public ResourceStatus getStatus() {
        return status;
    }

    public void setStatus(ResourceStatus status) {
        this.status = status;
    }

    public String getUseDate() { return useDate; }

    public void setUseDate(String useDate) { this.useDate = useDate; }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMarkdown() {
        return markdown;
    }

    public void setMarkdown(String markdown) {
        this.markdown = markdown.split("\n\uD83D\uDCCC")[0].trim();
    }
}
